// miniprogram/pages/Recycle/Recycle.js
const db=wx.cloud.database()
const Recycle=db.collection('Recycle')
var QQMapWX = require('../../qqmap-wx-jssdk');
var qqmapsdk;

var util=require('../../utils/util')
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    integral:'',
    value: '',
    weight:'',
    show: false,
    // input默认是1
		num: 1,
		// 使用data数据对象设置样式名
		minusStatus: 'disabled',

    addmissage: '选的位置',
    // markers	 Array	标记点
    stitle:'故宫',
    latitude: "",
    longitude: "",
    scale: 14,
    markers: [],
    //controls控件 是左下角圆圈小图标,用户无论放大多少,点这里可以立刻回到当前定位(控件（更新一下,即将废弃，建议使用 cover-view 代替）)
  
    controls: [{
      id: 1,
      iconPath: '../../images/airplane.png',
      position: {
        left: 20,
        top: 260 - 40,
        width: 30,
        height: 20
      },
       clickable: true
    }],
    // distanceArr: []
  },
  map_position:function(e){

     var that = this
    //获取当前的地理位置、速度
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        //赋值经纬度
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
 
        })
      }
    })
 
    console.log('1111')

  },
  recycle:function(event){
    db.collection('Recycle').add({
      data: {
        // amount: event.detail.value.amount,
        weight: event.detail.value.weight
      }
    }).then(res=>{
      // console.log('weight'+res.weight)
    })
  },

  showPopup() {
    this.setData({ show: true });
  },

  onClose() {
    this.setData({ show: false });
  },

  	/* 点击减号 */
	bindMinus: function() {
		var num = this.data.num;
		// 如果大于1时，才可以减
		if (num > 1) {
			num --;
		}
		// 只有大于一件的时候，才能normal状态，否则disable状态
		var minusStatus = num <= 1 ? 'disabled' : 'normal';
		// 将数值与状态写回
		this.setData({
			num: num,
			minusStatus: minusStatus
		});
	},
	/* 点击加号 */
	bindPlus: function() {
		var num = this.data.num;
		// 不作过多考虑自增1
		num ++;
		// 只有大于一件的时候，才能normal状态，否则disable状态
		var minusStatus = num < 1 ? 'disabled' : 'normal';
		// 将数值与状态写回
		this.setData({
			num: num,
			minusStatus: minusStatus
		});
	},
	/* 输入框事件 */
	bindManual: function(e) {
		var num = e.detail.value;
		// 将数值与状态写回
		this.setData({
			num: num
		});
  },
  onChange(event) {
    // event.detail 为当前输入的值
    console.log(event.detail);
  },

  // weight_input:function(event){
  //   if(event.detail.value.weight>=1&&event.detail.value.weight<=2){
  //     this.setData({
  //       integral:2
  //     })
  //     console.log('2')
  //   }

  // },
  
  HandleSubmit:function(event){
    if(event.detail.value.weight>=1&&event.detail.value.weight<=2){
      this.setData({
        integral:2
      })
      console.log('2')
    } 
    if(event.detail.value.weight>2&&event.detail.value.weight<=4){
      this.setData({
        integral:4
      })
      console.log('4')
    }
    if(event.detail.value.weight>4&&event.detail.value.weight<=6){
      this.setData({
        integral:6
      })
    }
    if(event.detail.value.weight>6&&event.detail.value.weight<=8){
      this.setData({
        integral:8
      })
    }
    if(event.detail.value.weight>8&&event.detail.value.weight<=10){
      this.setData({
        integral:10
      })
    }
    if(event.detail.value.weight!=="")
    { 
      db.collection('Recycle').add({
        data: {
          amount: event.detail.value.amount,
          weight: event.detail.value.weight,
          integral:this.data.integral,
          time:this.data.time
        }
      }).then(res=>{
       
        
        this.setData({ show: true });
        
        console.log('weight'+res.weight)
      })
    }else{
      wx.showToast({
        title: '请填写纸盒重量',
        icon:'none',
        image:'../../images/title.png',
        mask:'true'
      })
    }
    
  },
  
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    
    var time=util.formatTime(new Date())
    that.setData({
      time:time
    })

    //获取当前的地理位置、速度
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {
        //赋值经纬度
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
 
        })
      }
    })
 
 
 
  },
  //controls控件的点击事件
  bindcontroltap:function(event) {
    var lat = Number(40.544526);
    var lon = Number(81.297228);
    var bankName = '塔里木大学综合楼';
    console.log(lat);
    console.log(lon);
    wx.openLocation({
      type: 'gcj02',
      latitude: lat,
      longitude: lon,
      name: bankName,
      scale: 28
    })


    // var that = this;
    // if (e.controlId == 1) {
    //   that.setData({
    //     latitude: this.data.latitude,
    //     longitude: this.data.longitude,
    //     scale: 14,
    //   })
    // }
  },
  //导航
  onGuideTap: function (event) {
    var lat = Number(event.currentTarget.dataset.latitude);
    var lon = Number(event.currentTarget.dataset.longitude);
    var bankName = event.currentTarget.dataset.bankname;
    console.log(lat);
    console.log(lon);
    wx.openLocation({
      type: 'gcj02',
      latitude: lat,
      longitude: lon,
      name: bankName,
      scale: 28
    })
  },
 
 
 
})